package datasource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by Administrator on 2017/9/14.
 */
//创建数据源，只需要创建一次，然后每从里面取就可以了
public class C3P0Utils {
    //只需要创建一次？static 代码快 只加载一次
    private static ComboPooledDataSource cpds=null;
    static{
        //c3p0对数据源对象
        cpds=new ComboPooledDataSource();
        //设置与数据对关联
        try {
            cpds.setDriverClass("com.mysql.jdbc.Driver");
            cpds.setJdbcUrl("jdbc:mysql://localhost:3307/jdbc");
            cpds.setUser("root");
            cpds.setPassword("123456");
            //设置默认连接数
            cpds.setInitialPoolSize(5);
            cpds.setMaxPoolSize(15);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }
    //直接返回连接对象
    public static Connection getConnectiton() throws SQLException {
        return cpds.getConnection();
    }


}
